﻿using DomainService.IService.Sys;
using Infrastructure;
using Infrastructure.IRepository.Sys;
using Model.BusinessModel.OutPut;
using Model.DomainModel.Sys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Utility;

namespace DomainService.Service.Sys
{
    /// <summary>
    /// 用户服务接口实现
    /// </summary>
    public class SysUserService : ISysUserService
    {
        #region 构造实例化

        private readonly ISysUserRepository _sysUserRepository;

        public SysUserService(ISysUserRepository sysUserRepository)
        {
            _sysUserRepository = sysUserRepository;
        }

        #endregion

        /// <summary>
        /// 测试Autofac
        /// </summary>
        /// <returns></returns>
        public string TestAutofac()
        {
            return _sysUserRepository.TestAutofac();
        }

        /// <summary>
        /// 查询所有用户
        /// </summary>
        /// <returns></returns>
        public List<SysUser> GetAllUser()
        {
            return _sysUserRepository.GetAll(BaseSqlRepository.sysUser_selectAllSql);
        }

        /// <summary>
        /// 根据用户名称和密码获取用户信息
        /// </summary>
        /// <param name="userName">用户名称</param>
        /// <param name="password">用户密码</param>
        /// <returns></returns>
        public ReceiveStatus<LoginOutPut> GetUserMsg(string userName, string password)
        {
            ReceiveStatus<LoginOutPut> receiveStatus = new ReceiveStatus<LoginOutPut>();
            List<LoginOutPut> loginResultsList = new List<LoginOutPut>();
            if (string.IsNullOrEmpty(userName))
                return ExceptionHelper<LoginOutPut>.CustomExceptionData("用户名不能为空！");
            if (string.IsNullOrEmpty(password))
                return ExceptionHelper<LoginOutPut>.CustomExceptionData("密码不能为空！");
            var result = _sysUserRepository.GetUserMsg(userName, password);
            if (result == null)
                return ExceptionHelper<LoginOutPut>.CustomExceptionData(string.Format("用户【{0}】不存在,或账号密码输入错误", userName));
            if (result.IsOpen == false)
                return ExceptionHelper<LoginOutPut>.CustomExceptionData(string.Format("用户【{0}】已停用，请开启后再登录", userName));

            LoginOutPut loginResults = new LoginOutPut()
            {
                UserId = result.UserId,
                UserName = result.UserName,
                Token = string.Empty,
                ExpiresDate = string.Empty
            };
            loginResultsList.Add(loginResults);
            receiveStatus.data = loginResultsList;
            receiveStatus.msg = "登录成功";
            return receiveStatus;
        }
    }
}
